package com.hjqjl.common_lib.net;

import io.reactivex.FlowableTransformer;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/*
 * @author ：mp5a5 on 2019/4/14 17：33
 * @describe 用于RxJava线程切换
 * 用法
 * HttpMethods.getInstance().getJoke("1")
 *                     .compose(RxTransformerUtils.applyTransformer())
 *                     .compose(this.bindToLifecycle())
 *                     .subscribe(new BaseObserver<GetJoke>() {
 *
 *                         @Override
 *                         public void onSuccess(GetJoke response) {
 *                             ToastUtils.showShort(response.getMsg());
 *                             pVBinding.homeTvNetwork.setText("1==>" + new Gson().toJson(response));
 *                         }
 *                     });
 */
public class RxTransformerUtils {
    /**
     * 整合代替
     * .subscribeOn(Schedulers.io())
     * .observeOn(AndroidSchedulers.mainThread())
     */
    public static <T> ObservableTransformer<T, T> applyTransformer() {
        return tObservable -> tObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

    public static <T> ObservableTransformer<T, T> observableTransformer() {
        return tObservable -> tObservable.subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }

    public static <T> FlowableTransformer<T, T> flowableTransformer() {
        return upstream -> upstream.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }
}
